﻿using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;

namespace WeiSha.Core
{
	// Token: 0x02000002 RID: 2
	public class Compress
	{
		// Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
		public static string ZipDirectory(string folder)
		{
			return Compress.ZipDirectory(folder, "*.*", null);
		}

		// Token: 0x06000002 RID: 2 RVA: 0x00002070 File Offset: 0x00000270
		public static string ZipDirectory(string folder, string filePattern)
		{
			return Compress.ZipDirectory(folder, filePattern, null);
		}

		// Token: 0x06000003 RID: 3 RVA: 0x0000208C File Offset: 0x0000028C
		public static string ZipDirectory(string folder, string filePattern, string strZip)
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(folder);
			bool flag = !directoryInfo.Exists;
			if (flag)
			{
				throw new Exception("Directory does not exist");
			}
			bool flag2 = string.IsNullOrWhiteSpace(strZip);
			if (flag2)
			{
				strZip = directoryInfo.FullName + directoryInfo.Name + ".zip";
			}
			ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZip));
			zipOutputStream.SetLevel(6);
			Compress.zip(directoryInfo.FullName, filePattern, strZip, zipOutputStream);
			zipOutputStream.Finish();
			zipOutputStream.Close();
			return strZip;
		}

		// Token: 0x06000004 RID: 4 RVA: 0x00002118 File Offset: 0x00000318
		private static void zip(string folder, string filePattern, string StoreFile, ZipOutputStream s)
		{
			bool flag = folder[folder.Length - 1] != Path.DirectorySeparatorChar;
			if (flag)
			{
				folder += Path.DirectorySeparatorChar.ToString();
			}
			Crc32 crc = new Crc32();
			string[] fileSystemEntries = Directory.GetFileSystemEntries(folder, filePattern);
			foreach (string text in fileSystemEntries)
			{
				bool flag2 = text == StoreFile;
				if (!flag2)
				{
					bool flag3 = Directory.Exists(text);
					if (flag3)
					{
						Compress.zip(text, filePattern, StoreFile, s);
					}
					else
					{
						FileStream fileStream = File.OpenRead(text);
						byte[] array2 = new byte[fileStream.Length];
						fileStream.Read(array2, 0, array2.Length);
						string text2 = text.Substring(StoreFile.LastIndexOf("\\") + 1);
						ZipEntry zipEntry = new ZipEntry(text2);
						zipEntry.DateTime = DateTime.Now;
						zipEntry.Size = fileStream.Length;
						fileStream.Close();
						crc.Reset();
						crc.Update(array2);
						zipEntry.Crc = crc.Value;
						s.PutNextEntry(zipEntry);
						s.Write(array2, 0, array2.Length);
					}
				}
			}
		}

		// Token: 0x06000005 RID: 5 RVA: 0x00002260 File Offset: 0x00000460
		public static string ZipFile(string file)
		{
			return Compress.ZipFile(file, null);
		}

		// Token: 0x06000006 RID: 6 RVA: 0x0000227C File Offset: 0x0000047C
		public static string ZipFile(string file, string targetDir)
		{
			string text2;
			try
			{
				bool flag = !File.Exists(file);
				if (flag)
				{
					throw new Exception("File does not exist");
				}
				string fileName = Path.GetFileName(Path.ChangeExtension(file, ".zip"));
				bool flag2 = string.IsNullOrWhiteSpace(targetDir);
				if (flag2)
				{
					targetDir = file;
					bool flag3 = targetDir.IndexOf(Path.DirectorySeparatorChar) > -1 && targetDir[targetDir.Length - 1] != Path.DirectorySeparatorChar;
					if (flag3)
					{
						targetDir = targetDir.Substring(0, targetDir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
					}
				}
				bool flag4 = targetDir[targetDir.Length - 1] != Path.DirectorySeparatorChar;
				if (flag4)
				{
					targetDir += Path.DirectorySeparatorChar.ToString();
				}
				string text = Path.Combine(targetDir, fileName);
				bool flag5 = File.Exists(text);
				if (flag5)
				{
					File.Delete(text);
				}
				using (FileStream fileStream = File.OpenRead(file))
				{
					fileStream.Position = 0L;
					byte[] array = new byte[(int)fileStream.Length];
					fileStream.Read(array, 0, array.Length);
					fileStream.Close();
					using (FileStream fileStream2 = File.Create(text))
					{
						using (ZipOutputStream zipOutputStream = new ZipOutputStream(fileStream2))
						{
							zipOutputStream.SetLevel(6);
							ZipEntry zipEntry = new ZipEntry(Path.GetFileName(file));
							zipOutputStream.PutNextEntry(zipEntry);
							zipOutputStream.Write(array, 0, array.Length);
						}
					}
				}
				text2 = text;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return text2;
		}

		// Token: 0x06000007 RID: 7 RVA: 0x00002474 File Offset: 0x00000674
		public static string ZipFiles(List<string> fileList, string zipfile)
		{
			bool flag = !File.Exists(zipfile);
			if (flag)
			{
				FileInfo fileInfo = new FileInfo(zipfile);
				bool flag2 = !fileInfo.Directory.Exists;
				if (flag2)
				{
					fileInfo.Directory.Create();
				}
			}
			ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(zipfile));
			zipOutputStream.SetLevel(6);
			foreach (string text in fileList)
			{
				bool flag3 = !File.Exists(text);
				if (!flag3)
				{
					FileStream fileStream = File.OpenRead(text);
					byte[] array = new byte[fileStream.Length];
					fileStream.Read(array, 0, array.Length);
					string fileName = Path.GetFileName(text);
					ZipEntry zipEntry = new ZipEntry(fileName);
					zipEntry.IsUnicodeText = true;
					zipEntry.DateTime = DateTime.Now;
					zipEntry.Size = fileStream.Length;
					fileStream.Close();
					zipOutputStream.PutNextEntry(zipEntry);
					zipOutputStream.Write(array, 0, array.Length);
				}
			}
			zipOutputStream.Finish();
			zipOutputStream.Close();
			return zipfile;
		}

		// Token: 0x06000008 RID: 8 RVA: 0x000025B4 File Offset: 0x000007B4
		public static void UnZipFile(string zipfileName, string UnZipDir, string password)
		{
			ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(zipfileName));
			bool flag = password != null && password.Length > 0;
			if (flag)
			{
				zipInputStream.Password = password;
			}
			try
			{
				ZipEntry nextEntry;
				while ((nextEntry = zipInputStream.GetNextEntry()) != null)
				{
					string text = Path.GetDirectoryName(UnZipDir);
					string text2 = Path.GetDirectoryName(nextEntry.Name);
					string fileName = Path.GetFileName(nextEntry.Name);
					text2 = text2.Replace(":", "$");
					text = text + "\\" + text2;
					Directory.CreateDirectory(text);
					bool flag2 = fileName != string.Empty;
					if (flag2)
					{
						FileStream fileStream = File.Create(text + "\\" + fileName);
						byte[] array = new byte[2048];
						for (;;)
						{
							int num = zipInputStream.Read(array, 0, array.Length);
							bool flag3 = num > 0;
							if (!flag3)
							{
								break;
							}
							fileStream.Write(array, 0, num);
						}
						fileStream.Close();
					}
				}
				zipInputStream.Close();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				zipInputStream.Close();
			}
		}
	}
}
